package main

import (
	"chat/api"
	"chat/api/handler"
	"chat/config"
	"chat/consts"
	"chat/inits"
	"chat/internal/model"
	"chat/internal/queue"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
)

func main() {
	config.Init()
	inits.Init()
	inits.InitRabbitMq()
	go SendMessage()
	router := gin.Default()
	router.Static("/upload", "D:\\gowork\\src\\chat\\static\\upload\\")
	api.LoadRoutes(router)
	router.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

func SendMessage() {
	inits.InitRabbitMq()

	msgs := queue.PublishSub(consts.WS_ES)

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			var data model.SystemMessage
			_ = json.Unmarshal(d.Body, &data)
			for _, conn := range handler.OnlineUser {
				handler.WSRespSuccess(conn, data.Content)
			}
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
